package com.hxz.interceptor;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.hxz.entity.User;

/**
 * A哈哈哈copy的登录拦截器
 * 
 * @author Administrator
 *
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {

	private static final Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);

	/*
	 * 进入controller层之前拦截请求 返回值：表示是否将当前的请求拦截下来 false：拦截请求，请求别终止。true：请求不被拦截，继续执行
	 * Object obj:表示被拦的请求的目标对象（controller中方法）
	 */
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			HttpSession session)
			throws IOException {
		logger.debug("进入登录拦截器");
		User user = (User) session.getAttribute("USER_SESSION");
		if (user == null) {
			logger.info("查无此人");
			response.sendRedirect(request.getContextPath() + "/admin/login");// 拦截后跳转的方法
			return false;
		}
		logger.info("确有其人！");
		return true;
	}

	/*
	 * 处理请求完成后视图渲染之前的处理操作 通过ModelAndView参数改变显示的视图，或发往视图的方法
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) {
		logger.debug("执行了postHandle方法");
	}

	/*
	 * 视图渲染之后的操作
	 */
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		logger.debug("执行到了afterCompletion方法");
	}

}
